<?php
namespace app\index\controller;

use app\common\controller\Frontend;
use think\Session;
use think\helper\Time;
use app\admin\model\QuesStudent;
use app\admin\model\Student;
use app\admin\model\QuesTeacher;
use app\admin\model\Teacher;
use app\admin\model\User;

class Faculty extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'weiui';
    protected $model = null;
    protected $re_model = null;


    public function _initialize()
    {

        parent::_initialize();
        
    }


    public function index()
    {

        if($this -> request -> isPost()){
            $params = $this->request->post("row/a");
            $openid = Session::get('wechatUser')['openid'] ?? "";
            $this->model = new QuesTeacher();
            $this->re_model = new Teacher();
            $where['tel']= $params['tel'];
            $error_msg = '姓名、电话或工作填写有误,请检查！';


            $where['nickname'] = $params['nickname'];
            $per_row = $this->re_model->where($where)->find();
            if(!$per_row)
            {
                $this->error($error_msg,null,0);
            }

            $params['type'] = $per_row -> type ?? '3';

            if(isset($params['lon']) && $params['lon'] && isset($params['lat']) && $params['lat']){
                $address = getAddress($params['lon'],$params['lat']);

                if($address){
                    $params['wx_formatted_address'] = $address['formatted_address'];
                    $params['wx_city'] = $address['city'];
                    $params['wx_province'] = $address['province'];
                    $params['wx_district'] = $address['district'];
                    // $params['street'] = $address['street'];
                }
            }
            $temp_arr = ['4','5','6','7','8'];
            if (in_array($params['a_9'], $temp_arr))
            {
               $params['a_4'] = 2;
            }

            if(isset($params['a_10']) && $params['a_10'] == 0) {
                $params['a_10_1'] = 100;
                $params['a_10_2'] = 100;
            }
            $beginYesterday= mktime(0,0,0,date('m'),date('d')-3,date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            $params['add_time'] = strtotime($params['add_time']);
            if($params['add_time']>$endToday||$params['add_time']< $beginYesterday){
                $this->error('请选择合适的日期,上报日期只能为昨日和今日','',0);
            }
            $params['is_source'] = 2;
            $today = Time::today();
            $today_row = $this->model->where($where)->whereTime('create_time','between',$today)->find();
            if($today_row)
            {
                if($per_row->classes)
                {
                    $params['teacher_id']=$per_row->id;
                    $params['classes_id'] =  $per_row->classes->id ;
                    $params['classes_name'] = $per_row->classes->name;
                    // $params['year_'] = $row->year_;
                    $params['college_id'] = $per_row->classes->college_id;
                    if($per_row->classes->college)
                        $params['college_name'] = $per_row->classes->college->name;

                }
                $res = $today_row->allowField(true) -> isUpdate(true)->save($params);
                $this->success('上报成功','',0);
            }else {
                $params['openid'] = $openid;
                if($per_row->classes)
                {
                    $params['teacher_id']=$per_row->id;
                    $params['classes_id'] =  $per_row->classes->id ;
                    $params['classes_name'] = $per_row->classes->name;
                    // $params['year_'] = $row->year_;
                    $params['college_id'] = $per_row->classes->college_id;
                    if($per_row->classes->college)
                        $params['college_name'] = $per_row->classes->college->name;

                }

                $res = $this->model->allowField(true)->save($params);
                if ($res)
                    $this->success('上报成功','',0);
                else
                {
                    $this->error('上报失败','',0);
                    // $from_storage = 1;
                }

            }
        }

        $title = '疫情上报';
        $this -> assign('pageTitle',$title);

        $this -> assign('wxcode','ques');

        // $url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        // $this -> app->jssdk->setUrl($url);
        // $wxjs = $this -> app->jssdk->buildConfig(array('getLocation'), false);
        // $this -> assignConfig('wxjs',$wxjs);
        // $this -> assign('wxjs',$wxjs);

        return $this -> fetch();
    }

    public function getoside()
    {
        if($this -> request -> isPost()){
            $params = $this->request->post("row/a");
            $this->model = new QuesTeacher();
            $where['tel']= $params['tel'];
            $where['nickname'] = $params['nickname'];

            $today = Time::today();
            $today_row = $this->model->where($where)->whereTime('create_time','between',$today)->find();
            if($today_row)
            {
                $this->success('今日已上报');
            }
        }
    }
}